package educrypt.gui.components;

import java.awt.Component;
import java.awt.Window;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

import educrypt.gui.view.CryptanalysisPlayfairView;
import educrypt.i18n.Messages;

class CryptpanalysisPlayfairDialog extends EducryptDialog implements
		CryptanalysisPlayfairView {
	private static final long serialVersionUID = 6161791739809847346L;

	private final JPanel contentPanel = new JPanel();

	private JButton btnContinue;

	private JSpinner spMaxCandidates;

	private JSpinner spMaxGenerations;

	private JSpinner spMaxSurvivors;

	public CryptpanalysisPlayfairDialog(Window owner) {
		super(owner);
		this.setModal(false);

		setTitle(Messages.getString("guiPlayfairEvolutionary"));

		this.setSize(380, 280);
		this.setPreferredSize(contentPanel.getSize());
		getContentPane().setLayout(null);

		JLabel lblMaxCandidates = new JLabel(
				Messages.getString("guiMaxCandidates"));
		lblMaxCandidates.setBounds(30, 43, 222, 26);
		getContentPane().add(lblMaxCandidates);

		btnContinue = new JButton(Messages.getString("guiContinue"));
		btnContinue.setBounds(237, 207, 89, 23);
		getContentPane().add(btnContinue);

		JLabel lblMaxGenerations = new JLabel(
				Messages.getString("guiMaxGenerations"));
		lblMaxGenerations.setBounds(30, 157, 222, 26);
		getContentPane().add(lblMaxGenerations);

		JLabel lblMaxSurvivors = new JLabel(
				Messages.getString("guiMaxSurvivors"));
		lblMaxSurvivors.setBounds(30, 99, 222, 26);
		getContentPane().add(lblMaxSurvivors);

		spMaxCandidates = new JSpinner(new SpinnerNumberModel(5, 5, 1000, 5));
		spMaxCandidates.setBounds(273, 45, 53, 23);
		getContentPane().add(spMaxCandidates);

		spMaxGenerations = new JSpinner(
				new SpinnerNumberModel(50, 10, 1000, 10));
		spMaxGenerations.setBounds(273, 159, 53, 23);
		getContentPane().add(spMaxGenerations);

		spMaxSurvivors = new JSpinner(new SpinnerNumberModel(5, 5, 50, 1));
		spMaxSurvivors.setBounds(273, 101, 53, 23);
		getContentPane().add(spMaxSurvivors);
	}

	@Override
	public void open() {
		initializeValues();
		super.open();
	}

	@Override
	public void open(Component caller) {
		initializeValues();
		super.open(caller);
	}

	private void initializeValues() {
		spMaxCandidates.setValue(5);
		spMaxSurvivors.setValue(5);
		spMaxGenerations.setValue(50);
	}

	@Override
	public void addListener(ActionListener ctr) {
		btnContinue.setActionCommand(BREAK_PLAYFAIR);
		btnContinue.addActionListener(ctr);
	}

	@Override
	public int getMaxCandidates() {
		return (Integer) this.spMaxCandidates.getValue();
	}

	@Override
	public int getMaxGenerations() {
		return (Integer) this.spMaxGenerations.getValue();
	}

	@Override
	public int getSurvivors() {
		return (Integer) this.spMaxSurvivors.getValue();
	}
}
